package Admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.text.html.HTMLEditorKit.Parser;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;




import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class PDFBillPayment extends javax.swing.JDialog {

    private String  paymentid;
    private Payment pm;
    ConnectRMI connectRMI = new ConnectRMI();
    private String  checkepayment;
    private String  q, w, e, r, t, y, u;
    private String email;
    
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCheckepayment() {
        return checkepayment;
    }

    public void setCheckepayment(String checkepayment) {
        this.checkepayment = checkepayment;
    }

    public String getPaymentid() {
        return paymentid;
    }

    public void setPaymentid(String paymentid) {
        this.paymentid = paymentid;
    }

    public PDFBillPayment(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private void createPDF() {
        try {
            
            OutputStream file = new FileOutputStream(new File("C:/Thu Tien "+txtname.getText()+".pdf"));
            Document document = new Document();
            PdfWriter.getInstance(document, file);
            document.open();
            document.add(new Paragraph("Name :" + txtname.getText()));
            document.add(new Paragraph("Company Name :" + txtcompanyname.getText()));
            document.add(new Paragraph("Unit Price :" + txtunitprice.getText()+ " USD"));
            document.add(new Paragraph("Power :" + txtpower.getText()+ " USD"));
            document.add(new Paragraph("Water :" + txtwater.getText()+ " USD"));
            document.add(new Paragraph("Security :" + txtsecurity.getText()+ " USD"));
            document.add(new Paragraph("Washing Rooms :" + txtwashingrooms.getText()+ " USD"));
            document.add(new Paragraph("Clean Building Glasses :" + txtCleanBuildingGlasses.getText()+ " USD"));
            document.add(new Paragraph("Maintenance Elevator :" + txtmaintenanceelevator.getText()+ " USD"));
            document.add(new Paragraph("Amount Of Parking :" + txtamountofparking.getText()));
            document.add(new Paragraph("Date Of Payment :" + " " + txtdataofpayment.getText()));
            q = txtunitprice.getText();
            w = txtpower.getText();
            e = txtwater.getText();
            r = txtsecurity.getText();
            t = txtwashingrooms.getText();
            y = txtsecurity.getText();
            u = txtmaintenanceelevator.getText();

            Double Tong = Double.parseDouble(q) + Double.parseDouble(w) + Double.parseDouble(e)
                    + Double.parseDouble(r) + Double.parseDouble(t) + Double.parseDouble(y) + Double.parseDouble(u);

            document.add(new Paragraph("Total :" + Tong + " USD"));

            document.close();
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        lbname = new javax.swing.JLabel();
        txtname = new javax.swing.JTextField();
        lbcompanyname = new javax.swing.JLabel();
        txtcompanyname = new javax.swing.JTextField();
        lbpower = new javax.swing.JLabel();
        txtpower = new javax.swing.JTextField();
        lbwater = new javax.swing.JLabel();
        txtwater = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btsubmit = new javax.swing.JButton();
        btcancel = new javax.swing.JButton();
        cbbstatus = new javax.swing.JComboBox();
        lbdatepayment = new javax.swing.JLabel();
        txtdataofpayment = new javax.swing.JTextField();
        lbsecurity = new javax.swing.JLabel();
        txtsecurity = new javax.swing.JTextField();
        lbwashingrooms = new javax.swing.JLabel();
        txtwashingrooms = new javax.swing.JTextField();
        lbceanbuildingglasses = new javax.swing.JLabel();
        txtCleanBuildingGlasses = new javax.swing.JTextField();
        lbmaintenanceelevator = new javax.swing.JLabel();
        txtmaintenanceelevator = new javax.swing.JTextField();
        lbamountofparking = new javax.swing.JLabel();
        txtamountofparking = new javax.swing.JTextField();
        lbunitprice = new javax.swing.JLabel();
        txtunitprice = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        lbemail = new javax.swing.JLabel();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(java.awt.Color.red);

        lbname.setText("Name");

        lbcompanyname.setText("CompanyName");

        lbpower.setText("Power");

        lbwater.setText("Water");

        jLabel1.setText("Status");
        
        lbemail.setText("Email");
        
        btsubmit.setText("Submit");
        btsubmit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (cbbstatus.getSelectedItem() == "Paid") {
                    try {
                        EtowInterface obj = null;
                        Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
                        obj = (EtowInterface) registry.lookup(connectRMI.regis);
                        obj.updatestatus(pm.getA14() , txtdataofpayment.getText());
                        JOptionPane.showMessageDialog(null, "Paid");
                        setCheckepayment("close");
                        createPDF();
                        
                        obj.SendAndAttachFile(txtemail.getText() , "Thu Tien Hang Thang Dich Vu Thue ETown");

                        setVisible(false);
                    } catch (Exception ex) {

                    }
                } else {                  
                    try {
                        EtowInterface obj = null;
                        Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
                        obj = (EtowInterface) registry.lookup(connectRMI.regis);
                        obj.updatedate(pm.getA14() ,txtdataofpayment.getText());
                        createPDF();
                        setCheckepayment("close");
                        JOptionPane.showMessageDialog(null, "No Payment");
                        
                        obj.SendAndAttachFile(txtemail.getText() , "Thu Tien Hang Thang Dich Vu Thue ETown");

                        setVisible(false);
                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                }
            }
        });

        btcancel.setText("Cancel");

        btcancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
   
            }
        });

        lbdatepayment.setText("Date Of Payment");

        lbsecurity.setText("Security");

        lbwashingrooms.setText("WashingRooms");

        lbceanbuildingglasses.setText("CleanBuildingGlasses");

        lbmaintenanceelevator.setText("MaintenanceElevator");

        lbamountofparking.setText("AmountOfParking");

        lbunitprice.setText("UnitPrice");

        txtname.setText(pm.getA0());
        txtcompanyname.setText(pm.getA1());
        txtemail.setText(pm.getA2());
        txtunitprice.setText(pm.getA4());
        txtpower.setText(pm.getA5());
        txtwater.setText(pm.getA6());
        txtsecurity.setText(pm.getA7());
        txtwashingrooms.setText(pm.getA8());
        txtCleanBuildingGlasses.setText(pm.getA9());
        txtmaintenanceelevator.setText(pm.getA10());
        txtamountofparking.setText(pm.getA11());
        txtdataofpayment.setText(pm.getA12());
        
        txtname.setEnabled(false);
        txtcompanyname.setEnabled(false);
        txtemail.setEnabled(false);
        txtwater.setEnabled(false);
        txtunitprice.setEnabled(false);
        txtsecurity.setEnabled(false);
        txtpower.setEnabled(false);
        txtwashingrooms.setEnabled(false);
        txtCleanBuildingGlasses.setEnabled(false);
        txtmaintenanceelevator.setEnabled(false);
        txtamountofparking.setEnabled(false);
        
        cbbstatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No Payment", "Paid" }));
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lbdatepayment)
                            .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbamountofparking)
                                .addContainerGap())
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(lbmaintenanceelevator)
                                    .addContainerGap())
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lbceanbuildingglasses)
                                        .addContainerGap())
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(lbwashingrooms)
                                            .addContainerGap())
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(lbsecurity)
                                                .addContainerGap())
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(lbwater)
                                                    .addContainerGap())
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(lbpower)
                                                        .addContainerGap())
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(lbname)
                                                            .addContainerGap())
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addGroup(layout.createSequentialGroup()
                                                                .addComponent(lbemail)
                                                                .addContainerGap())
                                                            .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                    .addComponent(lbunitprice)
                                                                    .addComponent(lbcompanyname))
                                                                .addGap(58, 58, 58)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                    .addComponent(txtcompanyname)
                                                                    .addComponent(txtunitprice, javax.swing.GroupLayout.Alignment.LEADING)
                                                                    .addComponent(txtemail, javax.swing.GroupLayout.Alignment.LEADING)
                                                                    .addComponent(txtname, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                                                                    .addComponent(txtpower, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtwater, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtsecurity, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtwashingrooms, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtCleanBuildingGlasses, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtmaintenanceelevator, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtamountofparking, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(txtdataofpayment, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                                                    .addComponent(cbbstatus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                                        .addComponent(btsubmit)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(btcancel)))
                                                                .addContainerGap(18, Short.MAX_VALUE))))))))))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbname)
                    .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbcompanyname)
                    .addComponent(txtcompanyname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbemail)
                    .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtunitprice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbunitprice))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbpower)
                    .addComponent(txtpower, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbwater)
                    .addComponent(txtwater, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsecurity)
                    .addComponent(txtsecurity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbwashingrooms)
                    .addComponent(txtwashingrooms, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbceanbuildingglasses)
                    .addComponent(txtCleanBuildingGlasses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbmaintenanceelevator)
                    .addComponent(txtmaintenanceelevator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbamountofparking)
                    .addComponent(txtamountofparking, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbdatepayment)
                    .addComponent(txtdataofpayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbbstatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btsubmit)
                    .addComponent(btcancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PDFBillPayment.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PDFBillPayment.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PDFBillPayment.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PDFBillPayment.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PDFBillPayment dialog = new PDFBillPayment(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private javax.swing.JButton    btcancel;
    private javax.swing.JButton    btsubmit;
    private javax.swing.JComboBox  cbbstatus;
    private javax.swing.JLabel     jLabel1;
    private javax.swing.JLabel     lbamountofparking;
    private javax.swing.JLabel     lbceanbuildingglasses;
    private javax.swing.JLabel     lbcompanyname;
    private javax.swing.JLabel     lbdatepayment;
    private javax.swing.JLabel     lbmaintenanceelevator;
    private javax.swing.JLabel     lbname;
    private javax.swing.JLabel     lbpower;
    private javax.swing.JLabel     lbsecurity;
    private javax.swing.JLabel     lbunitprice;
    private javax.swing.JLabel     lbwashingrooms;
    private javax.swing.JLabel     lbwater;
    private javax.swing.JTextField txtCleanBuildingGlasses;
    private javax.swing.JTextField txtamountofparking;
    private javax.swing.JTextField txtcompanyname;
    private javax.swing.JTextField txtdataofpayment;
    private javax.swing.JTextField txtmaintenanceelevator;
    private javax.swing.JTextField txtname;
    private javax.swing.JTextField txtpower;
    private javax.swing.JTextField txtsecurity;
    private javax.swing.JTextField txtunitprice;
    private javax.swing.JTextField txtwashingrooms;
    private javax.swing.JTextField txtwater;
    private javax.swing.JTextField txtemail;
    private javax.swing.JLabel lbemail;
}
